<?php

    include('../../sidebardbinfo.php');
	// connect to the database
	$link = mysql_connect($dbhost, $dbuser, $dbpass);
	if (!$link) {
	    die('Problem connecting to database server : ' . mysql_error());
	}

	// make $dbname the current db
	$db_selected = mysql_select_db($dbname, $link);
	if (!$db_selected) {
	    die ('Problem connecting to database : ' . mysql_error());
	}

	$cronLog = "cronlog.txt";

	$fh = fopen($cronLog, 'a') or die("can't open cron log file");

	$logText = date('d-m-Y H:i:s');
	$logText .= " opening log file\n";
	fwrite($fh,$logText);


	$offset = 0;
	$alreadytherecount = 0;
	while ($alreadytherecount < 100) {
		$seesmicLatestVideosUrl = "http://v.seesmic.com/videos.json?offset=$offset&pagesize=50";
		$offset += 25;

		$cs = curl_init($seesmicLatestVideosUrl);
		curl_setopt($cs, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($cs, CURLOPT_USERAGENT, "seesmicsidebar"); //give your script an identity, so they can see you
		curl_setopt($cs, CURLOPT_HEADER, 0);
		curl_setopt($cs, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($cs, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
		curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 10);
		curl_setopt($cs, CURLOPT_TIMEOUT, 55);
		$result = curl_exec($cs);
		$resultArray = curl_getinfo($cs); // this can be useful to see http headers in the reply using print_r($resultArray)

		if (curl_errno($cs)) {
			$sid = curl_error($cs);
			curl_close($cs);
		    mysql_close($link);
			$logText = date('d-m-Y H:i:s');
			$logText .= " curl_error sid: ";
			$logText .= $sid;
			$logText .= "\n";
			$logText .= date('d-m-Y H:i:s');
			$logText .= " curl error, exiting cron and closing log file\n";
			fwrite($fh,$logText);
		    fclose($fh);
			exit;

		} else {
			$response = json_decode($result, true);
			if (is_array($response)) {
				foreach ($response as $array) {
					foreach ($array as $key => $value) {
						switch ($key) {
							case "video_id":
								$video_id = $value;
								break;
							case "source_server":
								$source_server = $value;
								break;
							case "thread_posts":
								$thread_posts = $value;
								break;
							case "title":
								$title = addslashes($value);
								break;
							case "language":
								$language = $value;
								break;
							case "username":
								$username = $value;
								break;
							case "url_flv":
								$url_flv = $value;
								break;
							case "thread_id":
								$thread_id = $value;
								break;
							case "reply_to_video_id":
								$reply_to_video_id = $value;
								break;
							case "source":
								$source = $value;
								break;
							case "reply_to_username":
								$reply_to_username = $value;
								break;
							case "url_mp4":
								$url_mp4 = $value;
								break;
							case "url_thumbnail":
								$url_thumbnail = $value;
								break;
							case "created_at":
								$created_at = $value;
								break;
							case "url_player":
								$url_player = $value;
								break;
						} // end switch ($key)

					} // end foreach ($array as $key => $value)

					// check if already in database
					$alreadytherequery = "SELECT video_id FROM videos WHERE video_id='$video_id'";
					$alreadythere = mysql_query($alreadytherequery);

					$num_rows = mysql_num_rows($alreadythere);
					// add if not
					if ($num_rows == 0) {
						$alreadytherecount = 0;
						$stmt = "INSERT INTO
									videos
								(
									video_id,
									source_server,
									thread_posts,
									title,
									language,
									username,
									url_flv,
									thread_id,
									reply_to_video_id,
									source,
									reply_to_username,
									url_mp4,
									url_thumbnail,
									created_at,
									url_player
								) VALUES (
									'$video_id',
									'$source_server',
									$thread_posts,
									'$title',
									'$language',
									'$username',
									'$url_flv',
									'$thread_id',
									'$reply_to_video_id',
									'$source',
									'$reply_to_username',
									'$url_mp4',
									'$url_thumbnail',
									'$created_at',
									'$url_player'
								)";
						$res = mysql_query($stmt);
						$logText = date('d-m-Y H:i:s');
						$logText .= " added ".$video_id."\n";
						fwrite($fh,$logText);

						$thumbnail_cache_uri = "images/thumbnails/".$video_id.".jpg";

						if (!file_exists($thumbnail_cache_uri)) {
							$contents = file_get_contents($url_thumbnail);

							$fp = fopen($thumbnail_cache_uri, 'w');
							fwrite($fp, $contents);
							fclose($fp);
						}
					} else {
						$alreadytherecount++;
					} // end if (!$alreadythere)

				} // end foreach ($response as $array)

			} // end if (!is_array($response))

		} // end if (curl_errno($cs))
	} // end while ($alreadytherecount < 200)

	curl_close($cs);
    mysql_close($link);

	$logText = date('d-m-Y H:i:s');
	$logText .= " cron finished OK, closing log file\n";
	fwrite($fh,$logText);
    fclose($fh);
?>